import { chatMap } from '@/constants/Index'
import { scrollToBottom } from '@/utils'
import { nextTick, ref } from 'vue'
import { dataIdDict } from './constants'

export function useChat(digmanURL: string) {
  const problem = ref('')
  const history = ref([
    {
      role: 'self',
      content: '哈喽，你好，我叫伶伦，我是您的虚拟导游，是由安图游开发定制的旅游专属数字人，现在你可以和我互动，我会告诉你我知道的一切。'
    }
  ])
  // 语音播报
  function play() {
    if (!problem.value) {
      // console.log('空内容')
      return
    }
    history.value.push({ role: 'tourist', content: problem.value })
    const answer = (chatMap as any)[problem.value]
    if (answer) {
      history.value.push({ role: 'self', content: answer.content })
      // // 给数字人传递信息
      // window.frames[0].postMessage({ p: dataIdDict[problem.value] }, digmanURL)
    }
    // 给数字人传递信息
    // console.log('给数字人传递信息：', dataIdDict[problem.value])
    window.frames[0].postMessage({ p: dataIdDict[problem.value] }, digmanURL)
    problem.value = ''
    nextTick(() => scrollToBottom('#chat-container'))
  }

  return {
    play,
    history,
    problem
  }
}
